﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace diploma.core
{
    public static class ByteSerialization
    {
        #region ToBytes

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="dt"></param>
        public static void DateTimeToBytes(System.IO.Stream stream, DateTime dt)
        {
            byte[] buf = BitConverter.GetBytes(dt.Ticks);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="d"></param>
        public static void DoubleToBytes(System.IO.Stream stream, double d)
        {
            byte[] buf = BitConverter.GetBytes(d);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="f"></param>
        public static void FloatToBytes(System.IO.Stream stream, float f)
        {
            byte[] buf = BitConverter.GetBytes(f);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="i"></param>
        public static void IntToBytes(System.IO.Stream stream, int i)
        {
            byte[] buf = BitConverter.GetBytes(i);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="l"></param>
        public static void LongToBytes(System.IO.Stream stream, long l)
        {
            byte[] buf = BitConverter.GetBytes(l);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="b"></param>
        public static void BoolToBytes(System.IO.Stream stream, bool b)
        {
            byte[] buf = BitConverter.GetBytes(b);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="ch"></param>
        public static void CharToBytes(System.IO.Stream stream, char ch)
        {
            byte[] buf = BitConverter.GetBytes(ch);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="sh"></param>
        public static void ShortToBytes(System.IO.Stream stream, short sh)
        {
            byte[] buf = BitConverter.GetBytes(sh);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="ui"></param>
        public static void UIntToBytes(System.IO.Stream stream, uint ui)
        {
            byte[] buf = BitConverter.GetBytes(ui);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="ul"></param>
        public static void ULongToBytes(System.IO.Stream stream, ulong ul)
        {
            byte[] buf = BitConverter.GetBytes(ul);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="us"></param>
        public static void UShortToBytes(System.IO.Stream stream, uint us)
        {
            byte[] buf = BitConverter.GetBytes(us);
            stream.Write(buf, 0, buf.Length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="s"></param>
        public static void StringToBytes(System.IO.Stream stream, string s)
        {
            ByteSerialization.IntToBytes(stream, s.Length);
            char[] str = s.ToCharArray();
            for (int i = 0; i < str.Length; i++)
            {
                byte[] buf = BitConverter.GetBytes(str[i]);
                stream.Write(buf, 0, buf.Length);
            }
        }
        #endregion

        #region FromBytes

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static DateTime DateTimeFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[8];
            stream.Read(buf, 0, 8);
            return new DateTime(BitConverter.ToInt64(buf, 0));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static double DoubleFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[8];
            stream.Read(buf, 0, 8);
            return BitConverter.ToDouble(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static float FloatFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[4];
            stream.Read(buf, 0, 4);
            return BitConverter.ToSingle(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static int IntFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[4];
            stream.Read(buf, 0, 4);
            return BitConverter.ToInt32(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static uint UIntFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[4];
            stream.Read(buf, 0, 4);
            return BitConverter.ToUInt32(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static long LongFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[8];
            stream.Read(buf, 0, 8);
            return BitConverter.ToInt64(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static ulong ULongFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[8];
            stream.Read(buf, 0, 8);
            return BitConverter.ToUInt64(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static short ShortFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[2];
            stream.Read(buf, 0, 2);
            return BitConverter.ToInt16(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static ushort UShortFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[2];
            stream.Read(buf, 0, 2);
            return BitConverter.ToUInt16(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static bool BoolFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[1];
            stream.Read(buf, 0, 1);
            return BitConverter.ToBoolean(buf, 0);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static char CharFromBytes(System.IO.Stream stream)
        {
            byte[] buf = new byte[2];
            stream.Read(buf, 0, 2);
            return BitConverter.ToChar(buf, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string StringFromBytes(System.IO.Stream stream)
        {
            int length = ByteSerialization.IntFromBytes(stream);
            string s = "";
            byte[] buf = new byte[2];
            for (int i = 0; i < length; i++)
            {
                stream.Read(buf, 0, 2);
                char ch = BitConverter.ToChar(buf, 0);
                s += ch;
            }
            return s;
        }

        #endregion
    }
}
